﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Gencode.Controls.CollapsibleControl;
using Gencode.PrsCommon.App.AppData;
using Gencode.PrsCommon.App;

namespace Personalplaner.Modules.Dienstplan
{
    [ToolboxItem(false)]
    public partial class DienstSelect : CollapsibleItem
    {
        DataView m_dienste;

		DiensplanSelector controler;
		public DiensplanSelector Controler
		{
			set
			{
				controler = value;
			}
		}

		public DienstSelect()
		{
			InitializeComponent();

			if (!this.DesignMode)
			{
				InitializeList();
			}
		}

        public DienstSelect(DiensplanSelector controler)
            : base("Dienste")
        {
            this.controler = controler;
            InitializeComponent();

            if (!this.DesignMode)
            {
                InitializeList();
            }
        }

		///
		/// Indicates if the current view is being utilized in the VS.NET IDE or not.
		///
		public new bool DesignMode
		{
			get
			{
				return (System.Diagnostics.Process.GetCurrentProcess().ProcessName == "devenv");
			}
		}

        public void InitializeList()
        {
            if (m_dienste != null)
                m_dienste.Dispose();
            m_dienste = Gencode.PrsCommon.Data.Dienst.DienstData.GetDienste(Verwaltung.Mandant, true);

			diensteGrid.AutoGenerateColumns = false;
			diensteGrid.DataSource = m_dienste;
			diensteGrid.Columns["id"].DataPropertyName = "Name";
            diensteGrid.Columns["besch"].DataPropertyName = "Kuzrtext";
        }

		public void InitializeDienstColor()
		{
			for (int i = 0; i < m_dienste.Table.Rows.Count; i++)
			{
				if (int.Parse(m_dienste[i].Row["RGB"].ToString()) != -1 &&
					int.Parse(m_dienste[i].Row["RGB"].ToString()) != 0)
				{
					try
					{
						diensteGrid[0, i].Style.BackColor = Color.FromArgb(int.Parse(m_dienste[i].Row["RGB"].ToString()));
					}
					catch { }
				}
			}
		}

		private void DienstSelect_Load(object sender, EventArgs e)
		{
			//set colors
			try
			{
				InitializeDienstColor();
			}
			catch { }
		}

		private void diensteGrid_CellClick(object sender, DataGridViewCellEventArgs e)
		{
			if (PlanUtil.Mandant == null || PlanUtil.Mandant == string.Empty)
				PlanUtil.Mandant = Verwaltung.Mandant;

			if (m_dienste != null &&
				m_dienste.Count > 0 &&
				diensteGrid.Rows.Count>0)
			{
				if (e.RowIndex < 0)
				{
					diensteGrid[0, 0].Selected = false;
					diensteGrid[1, 0].Selected = true;
				}

				PlanUtil.SelectedDienstID = int.Parse(m_dienste[e.RowIndex].Row["DienstID"].ToString());
			}
			else
				PlanUtil.SelectedDienstID = 0;

			if (e.ColumnIndex != 1)
			{
				diensteGrid[0, e.RowIndex].Selected = false;
				diensteGrid[1, e.RowIndex].Selected = true;
			}

			//Controler.PartnerContent
			if (controler != null)
				controler.PartnerView.Focus();
		}

		public int ControlHeight
		{
			get
			{
				int h = diensteGrid.Rows.Count * diensteGrid.RowTemplate.Height;

				return h;
			}
		}
    }
}
